Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dagrejs/graphlib

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dagrejs/graphlib

A directed and undirected multi-graph library

  • 2.2.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created

What is @dagrejs/graphlib?

@dagrejs/graphlib is a JavaScript library for creating and manipulating directed graphs. It provides a flexible and efficient way to work with graph data structures, including adding and removing nodes and edges, traversing the graph, and performing various graph algorithms.

What are @dagrejs/graphlib's main functionalities?

Creating a Graph

This feature allows you to create a new graph, add nodes, and add edges between nodes. The code sample demonstrates creating a graph, adding two nodes 'a' and 'b', and creating an edge from 'a' to 'b'.

const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new Graph();
g.setNode('a');
g.setNode('b');
g.setEdge('a', 'b');
console.log(g.nodes()); // ['a', 'b']
console.log(g.edges()); // [{ v: 'a', w: 'b' }]

Graph Traversal

This feature allows you to perform depth-first search (DFS) traversal on the graph. The code sample demonstrates creating a graph with three nodes and two edges, and then performing DFS starting from node 'a'.

const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new Graph();
g.setNode('a');
g.setNode('b');
g.setNode('c');
g.setEdge('a', 'b');
g.setEdge('b', 'c');
const dfs = graphlib.alg.dfs(g, ['a']);
console.log(dfs); // [{ v: 'a' }, { v: 'b' }, { v: 'c' }]

Finding Shortest Path

This feature allows you to find the shortest path in the graph using Dijkstra's algorithm. The code sample demonstrates creating a graph with three nodes and three weighted edges, and then finding the shortest path from node 'a' to all other nodes.

const graphlib = require('@dagrejs/graphlib');
const Graph = graphlib.Graph;
const g = new Graph();
g.setNode('a');
g.setNode('b');
g.setNode('c');
g.setEdge('a', 'b', { weight: 1 });
g.setEdge('b', 'c', { weight: 2 });
g.setEdge('a', 'c', { weight: 4 });
const dijkstra = graphlib.alg.dijkstra(g, 'a');
console.log(dijkstra); // { a: { distance: 0 }, b: { distance: 1 }, c: { distance: 3 } }

Other packages similar to @dagrejs/graphlib

Keywords

FAQs

Package last updated on 15 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc